<?php
namespace App\Services;

use App\Models\Cart;
use Illuminate\Support\Facades\DB;
//use Illuminate\Support\Facades\Session;
/**
 *
 * 购物流程服务层
 *
 * @author shengl@maqu.im
 *
 */
class CashierService extends BaseService {

    /**
     * 获取商品种类数
     *
     * @param $sess_id session id
     * @param $user_id 用户id
     * @return int $kinds 商品种类数
     */
    public function getKinds($sess_id, $user_id = 0)
    {
        $query = Cart::where('session_id','=',$sess_id);

        if($user_id){
            $query->where('user_id','=',$user_id);
        }

        $kinds = $query->distinct('goods_id')->count();

        return $kinds;
    }

    /**
     *以购物车为单位获取购物车列表及商品项
     *
     * @param int $user_id
     * @param int store_id
     * @param array $rec_ids
     * @return array
     */
    public function getGoodsByCart($user_id=0,$store_id=1,$rec_ids=[])
    {
        //获取购物车数据列表
        $query = DB::table('cart')
            ->join('store','cart.store_id','=','store.store_id')
            ->join('goods','goods.goods_id','=','cart.goods_id')
            ->join('goods_spec','cart.spec_id','=','goods_spec.spec_id')
            ->where('cart.store_id',$store_id);

        if($user_id){
            $query->where('cart.user_id',$user_id);
        //}else{
        //    $query->where('cart.session_id',Session::getId());
        }

        if(count($rec_ids)>0){
            //$query->where('tb_cart.rec_id in ('.implode(',',$rec_ids).')');
            $query->whereIn('cart.rec_id',$rec_ids);
        }

        $list = $query->select('cart.rec_id','cart.store_id','cart.goods_id','cart.goods_name','cart.spec_id',
                'cart.specification','cart.price','cart.quantity','cart.goods_image','cart.goods_type',
                'cart.extra_info','store.store_id','store.store_name','goods_spec.sku','goods_spec.spec_1',
                'goods.postage','goods_spec.price','goods_spec.stock','goods.is_set','goods.supplierid','store.store_name','store.config')
            ->get();

        //如果购物车内没有商品信息，则返回空数组
        if (empty($list)) {
            return [];
        }

        foreach($list as $object)
        {
            $cart_items[] =  (array) $object;
        }

        //编辑数据
        $goods_info =[
            'store_info' => [
                'store_id' => 0,
                'store_name' => '',
            ],
            'total_store' => [
                'kinds' => 0,
                'goods_amount' => 0,
                'quantity' => 0,
                'shipping_fee_info' => 0
            ],
            'goods' => []
        ];

        foreach ($cart_items as $k=> $item)
        {
            /* 编辑店铺信息 */
            if($goods_info['store_info']['store_id'] == 0 ){
                $goods_info['store_info']['store_id']    = $item['store_id'];
                $goods_info['store_info']['store_name']    = $item['store_name'];

                $configs= json_decode($item['config']);
                $config['shipping'] = (array)$configs->shipping;
                $goods_info['total_store']['shipping_fee_info'] = "满".$config['shipping']['min_free']."元包邮,不足金额运费".$config['shipping']['shipping_fee']."元";
            }

            //店铺小计
            $goods_info['total_store']['kinds']     += 1;
            $goods_info['total_store']['goods_amount']     += $item['price'] * $item['quantity'];   //各店铺的总金额
            $goods_info['total_store']['quantity']     += $item['quantity'];   //各店铺的总数量



            /* 商品缩略图 */
            empty($item['goods_image']) && $item['goods_image'] = env('default_goods_image');
            $item['goods_image'] = complete_url($item['goods_image']);

            /* 加入商品信息 */
            $goods_info['goods'][$k]['goods_id'] =$item['goods_id'];
            $goods_info['goods'][$k]['goods_name'] =$item['goods_name'];
            $goods_info['goods'][$k]['spec_id'] =$item['spec_id'];
            $goods_info['goods'][$k]['specification'] =$item['specification'];
            $goods_info['goods'][$k]['price'] =$item['price'];
            $goods_info['goods'][$k]['quantity'] =$item['quantity'];
            $goods_info['goods'][$k]['goods_image'] =$item['goods_image'];
            $goods_info['goods'][$k]['stock'] = $item['stock'];
            $goods_info['goods'][$k]['sku'] = $item['sku'];
            $goods_info['goods'][$k]['supplierid'] = $item['supplierid'];

        }

        return $goods_info;
    }

    /**
     *    获取配送方式列表
     *
     * @param   int $store_id
     * @param $firstrow
     * @param $pagesize
     * @return    array
     */
    public function getShippingList($store_id,$firstrow,$pagesize)
    {
        if (!$store_id)
        {
            return array();
        }
        return DB::table('shipping')->where('enabled',1)
            ->where('store_id',$store_id)
            ->select('shipping_id','shipping_name','first_price','step_price')
            ->skip($firstrow)->take($pagesize)
            ->get();
    }


    /**
     *    获取配送方式
     *
     *    @param     int $store_id
     *    @return    array
     */
    public function getShippingMethods($store_id)
    {
        if (!$store_id)
        {
            return [];
        }
        return DB::table('shipping')->where('enabled',1)
            ->where('store_id',$store_id)
            ->select('shipping_id','shipping_name','first_price','step_price')
            ->orderBy('shipping_id','asc')
            ->first();
    }

}
